package com.csit.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.User;
import com.csit.service.UserService;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.MD5Util;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
public class UserAction extends BaseAction implements ModelDriven<User> {

	private static final long serialVersionUID = -3573461851065676681L;
	
	private User user = new User();
	
	@Resource
	private UserService userService;
	
	public User getModel() {
		return user;
	}
	
	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(user.getUserCode())){
			msgUtil.setMsg("用户编号不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(user.getUserName())){
			msgUtil.setMsg("用户名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(user.getRoleType()==null){
			msgUtil.setMsg("用户角色不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(user.getShopID()==null){
			msgUtil.setMsg("用户必须属于某个店");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(userService.isEntityExist("userCode", user.getUserCode())){
			msgUtil.setMsg("用户编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		user.setPassword(MD5Util.digest("123456"));
		userService.saveEntity(user);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(user.getUserCode())){
			msgUtil.setMsg("用户编号不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(user.getUserName())){
			msgUtil.setMsg("用户名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(user.getRoleType()==null){
			msgUtil.setMsg("用户角色不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(user.getShopID()==null){
			msgUtil.setMsg("用户必须属于某个店");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		User model = userService.getEntity(user.getUserID());
		if(!userService.isEntityUnique("userCode",model.getUserCode(),user.getUserCode())){
			msgUtil.setMsg("用户编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		user.setPassword(model.getPassword());
		userService.updateEntity(user);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void updatePassword(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		String oldPassword = getParameter("oldPassword");
		if(StringUtil.isNull(oldPassword)){
			msgUtil.setMsg("旧密码不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(user.getPassword())){
			msgUtil.setMsg("新密码不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		Integer id = (Integer) getSession(User.LOGINID);
		User model = userService.getEntity(id);
		if(!model.getPassword().equals(MD5Util.digest(oldPassword))){
			msgUtil.setMsg("旧密码错误");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		model.setPassword(MD5Util.digest(user.getPassword()));
		userService.updateEntity(model);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		List<User> list = userService.getAll();
		String[] properties = {"userID","userCode","userName","roleType","shopID","shopName"};
		String jsonString = ListAndJsonUtil.getJSONString(list, properties);
		ajaxJson(jsonString);
	}
}
